package com.pounds.service.impl;

import com.pounds.mapper.UserInfoMapper;
import com.pounds.mapper.UserMapper;
import com.pounds.pojo.UserInfo;
import com.pounds.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserMapper userMapper;

    private enum UserInfoEnumError {
        Normal,
        DataFormatError,
        EmptyError,
        DatabaseError,
        DataNotFoundError,
    }

    @Override
    public List<UserInfo> GetUserInfo(String account) {
        return userInfoMapper.getUserInfo(account);
    }

    @Override
    public String SetUserInfo(String username, String account,String contact, String sex, int age, String introduce) {
        if (username == null || contact == null || account == null || sex == null || introduce == null){
            return UserInfoEnumError.DataFormatError.toString();
        }else if (username.isEmpty() || contact.isEmpty() || account.isEmpty() || sex.isEmpty() || introduce.isEmpty()){
            return UserInfoEnumError.EmptyError.toString();
        }
        String userAccount = userMapper.findUserAccount(account);
        if (userAccount == null){
            return UserInfoEnumError.DataNotFoundError.toString();
        } else if (userAccount.isEmpty()) {
            return UserInfoEnumError.DatabaseError.toString();
        } else if (account.equals(userAccount)){
            userInfoMapper.setUserInfo(username,account,contact,sex,age,introduce);
            return UserInfoEnumError.Normal.toString();
        }else{
            return UserInfoEnumError.DatabaseError.toString();
        }
    }

    @Override
    public String changeCollect(String account, String collect) {
        if (account == null || collect == null){
            return UserInfoEnumError.DataFormatError.toString();
        }else if(account.isEmpty() || collect.isEmpty()){
            return UserInfoEnumError.EmptyError.toString();
        }
        String userAccount = userMapper.findUserAccount(account);
        if (userAccount == null){
            return UserInfoEnumError.DataNotFoundError.toString();
        } else if (userAccount.isEmpty()) {
            return UserInfoEnumError.DatabaseError.toString();
        } else if (account.equals(userAccount)){
            userInfoMapper.changeCollect(account,collect);
            return UserInfoEnumError.Normal.toString();
        }else{
            return UserInfoEnumError.DatabaseError.toString();
        }
    }
}
